package zdh.avatar.gui;

import java.awt.Desktop;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import zdh.avatar.*;

/**
 *
 * @author Gem Seeker
 */
public class Menus extends JMenuBar implements ActionListener {
    
    private final JMenu add;
    private final JMenu edit;
    private final JMenu tools;
    private final JMenu show;
    private final JMenu help;
    
    // Add Menu Items
    
    private final JMenuItem addPanel;
    private final JMenuItem addButton;
    private final JMenuItem addButtonChild;
    
    // Edit Menu Items
    
    private final JMenuItem editPreferences;
    private final JMenuItem editPanel;
    private final JMenuItem editButton;
    
    // Tools Menu Items
    private final JMenuItem reports;
    private final JMenuItem register;
    
    // Show Menu Items
    private final JCheckBoxMenuItem welcomeDisplay;
    
    // Help Menu Items
    
    private final JMenuItem about;
    private final JMenuItem _help;
    
    private final MainFrame main;
    private final AvatarConfig config;
    
    public Menus(MainFrame main, AvatarConfig config){
        add = new JMenu("Add");
        add.setMnemonic(KeyEvent.VK_A);
        
        addPanel = new JMenuItem("Add Button Group");
        addPanel.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK));
        add.add(addPanel);
        
        addButton = new JMenuItem("Add Button");
        addButton.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, ActionEvent.CTRL_MASK));
        add.add(addButton);
        
        addButtonChild = new JMenuItem("Add Button Child");
        addButton.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, ActionEvent.CTRL_MASK + ActionEvent.SHIFT_MASK));
        add.add(addButtonChild);
        
        add(add);
        
        edit = new JMenu("Edit");
        edit.setMnemonic(KeyEvent.VK_E);
        
        editPanel = new JMenuItem("Edit Button Group");
        editPanel.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK + ActionEvent.SHIFT_MASK));
        edit.add(editPanel);
        
        editButton = new JMenuItem("Edit Button");
        editButton.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, ActionEvent.CTRL_MASK + ActionEvent.SHIFT_MASK));
        edit.add(editButton);
        
        editPreferences = new JMenuItem("Preferences");
        editPreferences.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK + ActionEvent.ALT_MASK));
        edit.add(editPreferences);
        
        add(edit);
        
        tools = new JMenu("Tools");
        tools.setMnemonic(KeyEvent.VK_T);
        
        reports = new JMenuItem("View Reports");
        reports.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.ALT_MASK));
        tools.add(reports);
        
        register = new JMenuItem("Register");
        register.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK));
        tools.add(register);
	register.setEnabled(false);
        
        add(tools);
        
        show = new JMenu("Show");
        welcomeDisplay = new JCheckBoxMenuItem("Welcome Display");
        if(config.getDisplayWelcomePanelOption().equals("true")){
            welcomeDisplay.setSelected(true);
        }
        show.add(welcomeDisplay);
        
        add(show);
        
        help = new JMenu("Help");
        help.setMnemonic(KeyEvent.VK_H);
        
        _help = new JMenuItem("Help");
        _help.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
        help.add(_help);
        
        about = new JMenuItem("About");
        about.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0));
        help.add(about);
        
        add(help);
        
        this.main = main;
        this.config = config;
        
        initListeners();
    }
    
    private void initListeners(){
        addPanel.addActionListener(this);
        addButton.addActionListener(this);
        addButtonChild.addActionListener(this);
        
        editPanel.addActionListener(this);
        editButton.addActionListener(this);
        editPreferences.addActionListener(this);
        
        reports.addActionListener(this);
	register.addActionListener(this);
        
        welcomeDisplay.addActionListener(this);
        
        about.addActionListener(this);
        _help.addActionListener(this);
    }
    
    @Override public void actionPerformed(ActionEvent e){
        Object source = e.getSource();
        
        if(source == addPanel){
            new AddEditGroup(main, config.getElementsPath(), 
                    AddEditGroup.ADD, null).showWindow();
        }
        
        else if(source == addButton){
            new AddEditButton(main, config, AddEditButton.ADD, null).showWindow();
        }
        
        else if(source == addButtonChild){
            new EditPanelButton(main, config, EditPanelButton.SET_CHILD_MODE).showWindow();
        }
        
        else if(source == editPanel){
            new EditPanelButton(main, config, EditPanelButton.PANELS_MODE).showWindow();
        }
        
        else if(source == editButton){
            new EditPanelButton(main, config, EditPanelButton.BUTTONS_MODE).showWindow();
        }
	
        else if(source == editPreferences){
            JOptionPane.showMessageDialog(null, "Not implemented yet.");
        }
        
        else if(source == about){
            JOptionPane.showMessageDialog(null, "Not implemented yet.");
        }
        
        else if(source == _help){
            JOptionPane.showMessageDialog(null, "Not implemented yet.");
        }
        
        else if(source == reports){
            Desktop desktop = Desktop.getDesktop();
            if(desktop.isSupported(Desktop.Action.OPEN)){
                try {
                    desktop.open(new File("reports"));
                } catch (IOException ex) {
                    System.err.println("Error opening reports directory." + e);
                    JOptionPane.showMessageDialog(null, "Error opening reports "
                            + "directory.\n" + e, "Error!", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
        
	else if(source == register){
	    JOptionPane.showMessageDialog(null, "Not implemented yet.");
	}
        
        else if(source == welcomeDisplay){
            if(welcomeDisplay.isSelected()){
                config.setDisplayWelcomePanel(true);
            }else{
                config.setDisplayWelcomePanel(false);
            }
            config.save();
            main.loadButtons();
        }
    }
    
    public void toggleShowWelcomeDisplayMenuItem(final boolean selected){
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                welcomeDisplay.setSelected(selected);
                config.setDisplayWelcomePanel(false);
                config.save();
            }
        });
    }
}
